<%@ page language="java"  pageEncoding="UTF-8"%>
<%@ page import = "java.util.*" %>
<%@ page import = "com.sinodata.bsm.common.vo.*" %>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@ include file="../include/taglib.jsp"%>
<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragrma","no-cache");
response.setDateHeader("Expires",0);
List<Attribute> attributes = (List<Attribute> )request.getAttribute("attributes");
Map<Long,String> map =(Map<Long,String>) request.getAttribute("attVals");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    
    <title>资源修改</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>    
    <%@ include file="../include/common.jsp"%>
    <%@ include file="../include/form.jsp"%>
	<%@ include file="../include/tree.jsp"%>
	<link rel="stylesheet" media="all" type="text/css" href="${pageContext.request.contextPath}/style/default/tree/zTreeStyle.css" />
   	<style type="text/css">
		.ztree {margin-top: 5px;border: 1px solid #617775;background: #fff;width:220px;height:350px;overflow-y:scroll;overflow-x:auto;}
	</style>
	
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery-ui.min.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/select/jquery.multiselect.min.js"></script>
	<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/style/default/select/jquery.multiselect.css" />
	<link rel="stylesheet"  type="text/css" href="${pageContext.request.contextPath}/style/default/jqueryui/jquery-ui.css" />
	
	<script type="text/javascript">
		function resetIt()
		{	
			document.forms[0].reset();
			$.formValidator.resetTipState("1")
			//resetRelTree();
		}
		function showRelTree() {
			var cityObj = $("#resRelName");
			var cityOffset = $("#resRelName").offset();
			var rTree = $.fn.zTree.getZTreeObj("relRelTree");
			if(rTree!=null){
				var nodes = rTree.getSelectedNodes();
				if(nodes.length==0){
					var defaultNode = rTree.getNodeByParam("id", '${relationType.id}', null);
					rTree.selectNode(defaultNode);
				}
				$("#relContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
				$("body").bind("mousedown", onRelTreeDown);
			}
		}
		function onRelTreeDown(event) {
			if (!(event.target.id == "menuBtn" || event.target.id == "resRelId" || event.target.id == "relContent" || $(event.target).parents("#relContent").length>0)) {
				$("#relContent").fadeOut("fast");
				$("body").unbind("mousedown", onRelTreeDown);
			}
		}
		
		function relNodeClick(e, treeId, treeNode) {
			$("#relContent").fadeOut("fast");
			$("body").unbind("mousedown", onRelTreeDown);
			$("#resRelId").attr("value", treeNode.id);
			$("#resRelName").attr("value",treeNode.name);
			$("#resRelName").blur();
		}
		var resRelTreeConfig = {
				async: {
					enable: true,
					url:"res-relation!typeTree.action",
					dataType: "text",
					autoParam:["id"]
				},
				callback: {
					onClick: relNodeClick
				},
				view: {
					dblClickExpand: false,
					showLine: false
				}
			};
		$(document).ready(function(){
			$.formValidator.initConfig({ 
				formid:"resForm",
				onerror:function(msg){},
				onsuccess:function(){
						$.ajax({
							type:'post',
							url:'res!update.action',
							data:$('#resForm').serialize(),
							global:false,
							async:false,
							cache:false,
							dataType:'json',
							success:function(data,textStatus){
								getTopWin(window).getMessageBox().tip('保存资源成功。', 'success',{timeout: 1000});
								var tree = window.parent.getResTree();
				 				var selectedNode = tree.getSelectedNodes()[0];
				 				selectedNode.name=$('#name').val();
				 				tree.updateNode(selectedNode);
								document.location.href = 'res-query!view.action?id=${res.id}&pId=${parentRes.id}&viewId=${viewId}';
							},
							error:function(){
								getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
							}
	   					   });
				     return false;
				}});
				
				$("#name").formValidator({onshow:"输入资源名称",onfocus:"资源名称最大长度为40"})
			              .inputValidator({min:1,onerror:"资源名称不能为空"})
			              .functionValidator({
			          	  fun:function(val,elem){
			          	     if(val.length>40){
			          	     	return "资源名称长度输入有误";
			          	     }
			          	     return true;
			          	  }
			             });
			
			var pId ='${parentRes.id}';
		
			if(pId!=''){
				$.fn.zTree.init($("#relRelTree"),resRelTreeConfig);
			}else{
				$('#resRelName').attr('disabled','true');
			}
		});
		
		//修改设备负责人
		$(function(){
			  var administrators = '${res.administrator}';
			  var us = new Array();
			  if(administrators!=''){
				  us = administrators.split(",");
			  }
			  $.ajax({
					url : '${pageContext.request.contextPath}/resource/res!getAllUsers.action',
					type : 'POST',
					dataType : "json",
					data : {},
					success : function(data) {
						$.each(data.users, function(i, index) {
							var flag = 0;
							for(var j=0;j<us.length;j++){
								if(index.userId.trim()==us[j].trim()){
									flag = flag +1;
								}
							}
							if(flag>0){
								$('#administrator').append('<option  value='+index.userId+' selected="select">' + index.userName+ '</option> ');
							}else{
								$('#administrator').append('<option  value='+index.userId+'>' + index.userName+ '</option> ');
							}
						});
						
						$('#administrator').multiselect({
							   selectedList: 5
						});
					}
				});
		});
	</script>
  </head>
  
  <body>
   <h2 class="title-h2" onclick="accordionDiv(this)"  ><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>修改资源</h2>
  <div id="resDiv">
	<form   name="resForm" id="resForm" method="post">
	<input type="hidden" name="id" id="id" value="${res.id }"  />
	<input type="hidden" name="pId" id="pId" value="${parentRes.id}" />
    <table class="table_eidt" id="restable" cellpadding="0" cellspacing="0">
		<tbody>
			<tr>
	    		<td class="td_left" style="width:150px">父资源</td>
	    		<td class="td_right" style="width:450px;">${parentRes.name}&nbsp;</td>
	    		<td class="td_left" style="width:150px">资源类型</td>
	    		<td class="td_right">
	    			<a href="res-type!model.action?id=${resType.id}" target="blank" title="点击链接可查看资源类型明细" >${resType.name}</a>
	    		</td>
	    	</tr>
	    	<tr>
	    		<td class="td_left">资源IP</td>
		    	<td class="td_right"><input type="text" name="ip" id="ip"   class="input_text" value='${res.ip }' /></td>
				<td class="td_left">监控代理</td>
	    		<td class="td_right">${probe.name}</td>
	    	</tr>
			<tr>
	    		<td class="td_left"><span class="span_alert"></span> 资源编码</td>
	    		<td class="td_right">
						<input type="text" name="code" id="code"   class="input_text" value='${res.code }' />
						<span id="codeTip"></span>
				</td>
				<td class="td_left">内部标示</td>
	    		<td class="td_right"><input type="text" name="internalId" id="internalId"   class="input_text" value='${res.internalId}' /></td>
	    	</tr>
	    	<tr>
	    		<td class="td_left"><span class="span_alert">*</span> 资源名称</td>
	    		<td class="td_right">
						<input type="text" name="name" id="name"   class="input_text" value='${res.name }' />
						<span id="nameTip"></span>
				</td>
				<td class="td_left">
					<span class="span_alert">*</span> 与父资源关系
				</td>
	    		<td class="td_right">
					<input type="text" name="resRelName" id="resRelName"  value="${relationType.name}" class="input_text" onfocus="showRelTree(); return false;"/>
					<input type="hidden" name="resRelId" id="resRelId" value="${relationType.id}" />
				</td>
	    	</tr>
	    	<tr id="codeTr" >
	    	<%
	    		int index =1 ;
	    		for(int i=0;i<attributes.size();i++){
		    		Attribute att = attributes.get(i);
		    		
					if(index==3){
						out.println("<tr>");
					}		
	    	%>
	    		<td class="td_left">
					 <%=att.getName() %>
				</td>
	    		<td class="td_right" >
	    		<%
	    			ResType resType =(ResType)request.getAttribute("resType");
	    			    String attrVal = map.get(att.getId())==null ? "": map.get(att.getId());
	    		%>
	    		<input type="text" name="prop_<%=att.getId() %>" id="prop_<%=att.getId() %>" value="<%=attrVal.replace("\"","&quot;") %>" class="input_text"  />
				</td>
	    	<% 		
	    			index++;
		    		if(i==1){
		    			out.println("</tr>");
		    		}else if(index==5){
		    			out.println("</tr>");
		    			index=3;
		    		}
	    		}
				
				out.println("</tr>");
	    	%>
	    	<!-- 设备负责人 -->
	    	<tr>
	    		<td class="td_left">
					 设备负责人
				</td>
	    		<td class="td_right" colspan="3">
					<select id="administrator" name="administrator" multiple="multiple" size="1"  class="select" style="height:30px;width:72%;text-align:center;">
		      	   	</select>
				</td>
			</tr>
			<!-- 设备负责人 -->
	    	<tr>
	    		<td class="td_left">
					 描述
				</td>
	    		<td class="td_right" colspan="3" >
					<textarea class="input_textarea" id="desc" name="desc" rows="3" style="width:83%;" onkeydown="this.value=this.value.substring(0,250)" >${res.desc }</textarea>
				</td>
			</tr>
		</tbody>
    </table>
    			
    	<div   class="btn-div">			
   			<span id="btgroup" >
   				<button type="submit" id ="saveBt" value="保存" class="btn-4">保存</button>
   				<button type="button" onclick='resetIt();' class="btn-4">重置</button>
   				<button type="button" onclick='history.back()' class="btn-4">返回</button>
   			</span>
    	</div>	
   </form>
</div>
	<div id="relContent" class="menuContent" style="display:none; position: absolute;">
	  <ul id="relRelTree" class="ztree" style="margin-top:0;padding-left:0;  width:220px; height:120px; "></ul>
   </div>
  </body>
</html>
